package huffman;

//Basic node in a Huffman coding tree.
public class HuffNode implements Comparable<HuffNode> {

	public int value;
	public int weight;

	protected HuffNode left;
	protected HuffNode right;
	protected HuffNode parent;

	HuffNode(int v, int w, HuffNode lt, HuffNode rt, HuffNode pt) {
		value = v;
		weight = w;
		left = lt;
		right = rt;
		parent = pt;
	}

	public int compareTo(HuffNode other) {
		if (weight < other.weight) {
			return -1;
		} else if (weight == other.weight) {
			return 0;
		} else {
			return 1;
		}
	}


	@Override
	public String toString() {
		return toString("");
	}

	private String toString(String indent) {
		String indentStep = "    ";
		StringBuilder buffer = new StringBuilder();
		buffer.append("[").append("" + this.value + " : " + this.weight);
		if (this.left != null || this.right != null) {
			buffer.append("\n");
			if (this.left != null) {
				buffer.append(indent)
						.append("  (")
						.append(0)
						.append(")--> ")
						.append(this.left.toString(indent + indentStep));
			}
			if (this.right != null) {
				buffer.append(indent)
						.append("  (")
						.append(1)
						.append(")--> ")
						.append(this.right.toString(indent + indentStep));
			}

		}

		buffer.append(indent + "]\n");
		return buffer.toString();
	}


}